Accessibility

DRK Volume 4 Article

Log4CF Tag

Log4CF is a ColdFusion logging utility that allows you to exercise significantly greater control over application logging than you can with the existing CFLOG tag. Log4CF creates unique log files—both at the server level and the application level—that you can view and manipulate within the logging section of the ColdFusion Administrator.

Logging comprises a crucial portion of the development and maintenance of any application—especially distributed applications where there is no simple compilation and debugging process. Many developers utilize logging to track their applications, looking for issues that arise during development and testing in production environments. Although ColdFusion MX has a number of logging features built in, including the CFLOG tag that allows developers to create their own log entries, it is still a rudimentary system. CFLOG users must remove or comment out CFLOG tags when an application is deployed (or have it continually log everything) and generally don't have much control over their applications' use of logging.

Here's how Log4CF improves your logging abilities:

  • Logging is controlled by severity. Every log line is given a level, in ascending order of severity: Debug, Information, Warning, Error, and Fatal. You can control whatever is actually logged at the server and application level. For example, in the development environment, you can set the logging level for your application to Debug. This way all Log4CF tags set at that level or higher will be logged. In the production environment, however, only items set to the level of Warning or higher will be logged, thus allowing you to keep your log tags in the CFML templates without worrying about creating huge, unreadable log files on the production server.
  • Logging information is entered in the tag body, not an attribute value. This allows you to log virtually anything, including CFDUMP tables, HTML data, and dynamic information.
  • Logs can be e-mailed. Whether you're an administrator or developer, you can have log information e-mailed to yourself.
  • Log-setting administration occurs in the ColdFusion Administrator or in an XML file. You can edit all of Log4CF's settings either with a web-based interface in the ColdFusion Administrator or by editing the reader-friendly log4cf.xml configuration file manually.
  • New log files are created. A primary log file (log4cf.log) is created at the server level to handle any CFML templates not in ColdFusion applications or in applications not entered manually into the Log4CF configuration file. Any applications that are entered into the Log4CF configuration file have their own log file (application_name.log).
  • Logs are completely integrated with the ColdFusion Administrator. All of the logs created by Log4CF can be viewed, archived, downloaded, or deleted in the logging section of the ColdFusion Administrator. This includes color coding severity levels, sorting and filtering log information, and other crucial log analysis features.
  • Log information can be outputted in the page. In addition to writing to the log file and e-mailing log information, you can choose to have the log information outputted directly in the page, while still controlling it by the severity level set in the log configuration file. This feature is helpful during development.

Installing Log4CF

It's relatively straightforward to install Log4CF:

  1. Extract the CFIDE folders on the DRK into the CFIDE folder of your ColdFusion server (where the ColdFusion Administrator resides). If you have an existing copy of extensionscustom.cfm you should add the HTML contents of the enclosed extensionscustom.cfm into your own file and not overwrite your existing file.
  2. Extract the CustomTags folder contents on the DRK into the CustomTags folder in the ColdFusion MX installation folder. (The standalone default folder in Windows is C:\cfusionmx\CustomTags\.)
  3. Extract the lib folder contents on the DRK into the lib folder in the ColdFusion MX installation folder.
  4. Extract the log4cf folder on the DRK into your ColdFusion root in your web server. This contains the documentation (index.html) and test file (test.cfm).

The CF_Log4CF Tag API

The CF_Log4CF custom tag is used to enable logging in ColdFusion applications.

Syntax
<cf_log4cf
   level = "Debug|Information|Warning|Error|Fatal"
   showinpage = "yes|no">Message to log</cf_log4cf>
Attributes

Attribute Req/Opt Default Description
Level Required n/a Level (severity) of the message:
  • Debug
  • Information
  • Warning
  • Error
  • Fatal
Not only is the severity logged but the severity level determines whether or not this information is written to the log file. See the administration information later in this article.
ShowInPage Optional No Yes or No: determines if the data in the tag body will be output to the requesting page.

Usage

Log4CF writes logging information to custom logs managed by the ColdFusion Administrator. Each line of the log contains the following comma-delimited information:

    Severity Thread Date Time Application Logged text

Values are enclosed in double quotation marks. Severity depends on whatever is specified by the LEVEL attribute in the syntax. Thread only contains file information for sorting purposes; date and time are the date and time that the line was written to the log; and logged text is whatever information—including HTML, etc.—is passed to the tag body. The application information (unlike regular CF logs) is in the following format:

(application_name)/calling_template/:line_number

You can set the level of logging in the ColdFusion Administrator or by editing the log4cf.xml configuration file. See the sections on administration later in this document.

The following example logs the username once the person logs into an application:

<cf_log4cf level="Debug" showinfile="Yes">
   <cfoutput>#GetAuthInfo()#</cfoutput>
</cf_log4cf> 

This could yield the following log file:

"Debug","_myapp_login_cfm","08/15/2003","12:43:20","jtRipper"

Administering Log4CF Using the ColdFusion Administrator

Log4CF allows you to set a number of properties that affect how Log4CF operates. You can edit or add settings at both the server and application levels. Figure 1 shows the main screen of the Log4CF administrator.


Figure 1. The main Log4CF administrator screen.

Here's the information you can edit at the server level:

  • Severity level to log
  • Enable e-mailing of log information
  • E-mail address to send information to

You can edit this information from the main Log4CF screen. The severity level determines what cf_log4cf tag calls to write to the log. Ranked in order of increasing severity, they are Debug < Information < Warning < Error < Fatal. This level applies to any ColdFusion template not in an application, or any CF template in any application that is not specifically added to the CF_Log4CF configuration file. (See below for information at the application level.) If the LEVEL attribute of a cf_log4cf tag is equal to or greater than the severity setting, then the log line will be written and the e-mail sent—if you have enabled that feature. To do so, you must select the Enable Emailing check box and enter an e-mail address to send the information to (see Figure 2).


Figure 2. The Server Settings section of the Log4CF administrator.

Here's the information you can edit at the application level:

  • Enable logging at application level (if disabled will default to server level)
  • Severity level to log
  • Enable e-mailing of log information
  • E-mail address to send information to

Editing applications is much like making changes to server-level settings. The main things to remember are that application settings (if enabled) override server settings. Also, the application name you enter in the CF_Log4CF administrator (or configuration file) must match exactly the application name defined the ColdFusion application's <cfapplication> tag. Once you enter it, you will not be able to change the application's name. If you make a mistake, delete that application from the administrator and reenter the name correctly.

The main screen of the Log4CF administrator shows all the applications entered into the configuration file and whether they are enabled or disabled. (Essentially, the Log4CF administrator is a user-friendly way of editing the log4cf.xml file.) By clicking the appropriate link in this table (see Figure 3), you can either delete an application from Log4CF or edit its logging settings.


Figure 3. Applications in Log4CF shown in the main Log4CF administrator screen.


Figure 4. The Edit Application Settings section of the Log4CF administrator.

Note: For information on viewing, downloading, searching, and otherwise working with log files, refer to Administering ColdFusion MX in the ColdFusion MX documentation.

Administering Log4CF Using Log4cf.xml

In addition to editing Log4CF settings in the ColdFusion Administrator, brave users can edit the configuration file directly. This file, called log4cf.xml, is located in the {cfmx_root}/lib/ directory. Because this is an advanced technique, and any errors in editing the XML file directly could cause Log4CF to create errors, please back up your log4cf.xml file before editing it.

The root element of this file, log4cf, has no attributes. Under this element are two child elements:

  • config (no attributes): stores server information.
  • application: stores information about each application administered by Log4CF. This element has two attributes: enabled (true or false) determines if the log settings are enabled and name is the application name.

Both child elements config and application have the same two children:

  • email: stores e-mail information. The attribute enabled (true or false) determines whether e-mails will be sent to the address specified in the address attribute.
  • level: stores the severity level to begin logging (also logs all higher levels).

Log4CF Tips

Because Log4CF writes to the log directory of ColdFusion MX, be sure that ColdFusion MX has the necessary permissions to write to the {cfmx_install_root}/logs/ directory. Otherwise you will get an error executing cf_log4cf.

Log4CF uses the SERVER.log4cf structure to store information about log4cf settings. You should be aware of potential conflicts or issues that might arise using the SERVER scope.

Currently, if a log file is archived in the ColdFusion Administrator, logging information added to the new log will not be viewable in the ColdFusion Administrator. To work around this, rename the old log file manually instead (to applicationName.log.1 or something). Log4CF will automatically detect that the file is missing and create a new one.

Log4CF improves a developer's or administrator's ability to monitor applications granularly. The simple API of this tag integrates with the existing ColdFusion logging paradigm, making it easy to adopt.

Happy logging!


About the Author


Drew Falkman is the author of JRun Web Application Construction Kit and co-author (with Ben Forta) of Reality ColdFusion: J2EE Integration, both published by Macromedia Press. Currently Drew offers technology planning, project management, and development services through his consulting company Veraison, LLC. Drew also speaks at events, writes for numerous publications, and teaches web development courses at Portland State University. Over the past seven years, Drew has developed over 150 web applications in all sizes primarily using ColdFusion and Java. Most recently, Drew guided Stampede Cattle Company's technical direction as "virtual CTO," including planning and managing the development of their real-time cattle auction (www.stampedecattle.com) using Macromedia Flash Remoting and Flash Communication Server. This site was featured on NPR's "All Things Considered" and other national press venues. In addition, Drew is a member of Team Macromedia, a certified ColdFusion Developer, and a certified Macromedia instructor. You can learn more about Drew at www.drewfalkman.com.